# _*_ coding: utf-8 _*_
from odoo import api, fields, models, tools


class ZfyStockMoveLots(models.Model):
    _inherit = "stock.move.lots"
    # _auto = False
    # _order = 'create_date asc'

    @api.onchange('production_id')
    def _onchange_production_id(self):
        # if self.production_id:
        #     self.quantity = self.production_id.move_finished_ids[0].quantity_done
        for rcd in self:
            if rcd.production_id:
                print rcd.production_id.move_finished_ids[0]
                print rcd.production_id.move_finished_ids[0].product_id

                for move in rcd.production_id.move_finished_ids:
                    print move.lot_ids
                    self.product_id = move.product_id
                    if move.state=='confirmed':
                        self.move_id = move.id
                        self.quantity=move.product_qty
                        self.ref = move.ref
                    # if self.quantity_done:
                    #     continue
                    # else:
                    #     self.quantity=
                    # self.product_uom_qty = move.product_uom_qtyMO/SJ/2018092502552
                    #select sml.quantity,* from mrp_production mp left join stock_move sm on sm.production_id=mp.id
#left join stock_move_lots sml on sml.move_id = sm.id where mp.name='MO/SJ/2018092502552'

class ZfyMrpProduction(models.Model):
    _inherit = "mrp.production"


    @api.multi
    def action_view_barcode(self):
        '''
        barcode view
        '''
        action = self.env.ref('product_barcode.action_mrp_barcode').read()[0]
        moves = self.mapped('move_finished_ids')
        if len(moves) >= 1:
            action['domain'] = [('move_id', 'in', moves.ids)]
            action['context'] = ['search','=',None]
        # elif moves:
        #     action['views'] = [(self.env.ref('stock.view_move_form').id, 'form')]
        #     action['res_id'] = moves.id
        else:
            action = {'type': 'ir.actions.act_window_close'}
        return action
